package cn.edu.suda.catalina.dataupdate.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by admin on 2017/5/23.
 */
public class RegexUtil {
    /**
     * 参数下标
     */
    private static int subscript;
    private static List<Object> list;
    private static BufferedReader bufferedReader;

    /**
     * 读取数据文件
     * @param pathName
     * @return
     * @throws IOException
     */
    public static String[] readLineAsArray(String pathName) throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(pathName), "GBK"));
        String line;
        if ((line = bufferedReader.readLine()) != null) {
            return line.split("#");
        }
        return null;
    }

    /**
     * 修改源文件
     * @param filePath
     * @param className
     * @return
     */
    public static List<Object> modify(String filePath, String className) {
        PrintStream printStream;
        subscript = 0;
        list = new ArrayList<>();
        try {
            bufferedReader = new BufferedReader(new FileReader(originalFile(filePath, className)));
            printStream = new PrintStream(new FileOutputStream(editedFile(filePath, className)));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                printStream.println(startsWith(endsWith(line)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 源文件
     * @return
     */
    private static File originalFile(String filePath, String className) {
        String originalName = ClassNameUtil.getBakName(className);
        return new File(filePath, originalName);
    }

    /**
     * 目标文件
     * @return
     */
    private static File editedFile(String filePath, String className) {
        String editedName = ClassNameUtil.getSimpleName(className);
        return new File(filePath, editedName);
    }

    /**
     * 判断字符串是否以特殊字符串开头
     * @param str
     * @return
     */
    private static String startsWith(String str) {
        String target;
        if (str.startsWith("package")) {
            target = "package cn.edu.suda.catalina.dataupdate.script;";
        } else if (str.startsWith("public class")) {
            target = str.replace("{", "implements BaseScript, Serializable {");
        } else if (str.trim().startsWith("@")) {
            target = str.replace("@", "//");
        } else if (str.trim().startsWith("public void test")) {
            target = str.replaceAll("test[^\\s]*\\(\\)", "test(String... args)");
        } else if (str.trim().startsWith("driver.get")) {
            target = "driver.get(baseUrl);";
        } else {
            target = str;
        }
        return target;
    }

    /**
     * 判断字符串是否以特殊字符串结尾
     * @param str
     * @return
     */
    private static String endsWith(String str) {
        String target;
        Matcher matcher;
        if (str.endsWith("firefox.FirefoxDriver;")) {
            target = str.replace("firefox.FirefoxDriver", "chrome.ChromeDriver") + "\nimport org.openqa.selenium.chrome.ChromeOptions;";
        } else if (str.endsWith("new FirefoxDriver();")) {
            target = "\tChromeOptions chromeOptions = new ChromeOptions();\n\tchromeOptions.addArguments(\"--start-maximized\");\n\tdriver = new ChromeDriver(chromeOptions);";
        } else if (str.endsWith(".click();")) {
            target = str + "\n\ttry {\n\t\tThread.sleep(1000);\n\t} catch (InterruptedException e) {\n\t\te.printStackTrace();\n\t}";
        } else if (str.endsWith("\");")) {  //参数修改
            String regex = "\\(\"[^\"]*\"\\);";
            matcher = Pattern.compile(regex).matcher(str);
            if (matcher.find()) {
                //添加参数到链表
                list.add(matcher.group().split("\"")[1]);
                target = str.replaceAll(regex, "(args[" + subscript++ + "]);");
            } else {
                target = str;
            }
        } else {
            target = str;
        }
        return target;
    }
}
